import { objectToString } from '@vue/shared';
import { defineStore } from 'pinia'
export interface CartType {
  objectId: string;
  name: string;
  price: number;
  img: string;
  n: number;
  num: string
}
export interface CartLeft {
  name: string
}
export interface CartState {
  list: CartType[],
  payAll: [],
  payFail: [],
  paySuc: [],
  all: {
    drink: CartType[],
    hot: CartType[],
    menu: CartType[],
    recommend: CartType[],
    selected: CartType[],
    staple: CartType[],
  },
  left: CartLeft[]
}
export const useOrderDel = defineStore('cart', {
  state: (): CartState => ({
    list: [
      // {
      //   img: "http://lc-Bujw8nis.cn-n1.lcfile.com/HPS4CzsgDbuHlMSkIgusIL5EowJ6OtX5/pic.png",
      //   objectId: "636163769ab7fe3cc511f1f1",
      //   name: "82年拉菲",
      //   price: 2,
      //   n: 0,
      //   num: "月售2份",
      // }
    ],
    payAll: [],
    payFail: [],
    paySuc: [],
    all: {
      recommend: [

      ],

      hot: [

      ],
      selected: [

      ],
      menu: [

      ],

      staple: [

      ],
      drink: [

      ],
    },
    left: [
      { name: "精选推荐" }, { name: "人气热卖" }, { name: "精选套餐" },
      { name: "所有菜单" }, { name: "主食" }, { name: "酒水饮料" }
    ]

  }),

  getters: {
    cartAllInfo() {
      let allNum = 0;
      let Allprice = 0;
      this.list.forEach((item) => {
        allNum += item.n;
        (Allprice) += item.n * parseInt(String(item.price)) * 100;
      })
      return {
        allNum,
        Allprice
      }
    }
  },
  actions: {
    cartAddAction(obj: any) {
      for (let i = 0; i < this.list.length; i++) {
        if (this.list[i].objectId == obj.objectId) {
          this.list[i].n++;
          localStorage.setItem('carlist', JSON.stringify(this.list))
          console.log(this.list[i])
          console.log(this.list)
          return
        }
      }
      this.list = [...this.list, obj]
      localStorage.setItem('carlist', JSON.stringify(this.list))
      // console.log(obj)
    },
    cartMunisAction(obj: any) {
      for (let i = 0; i < this.list.length; i++) {
        if (this.list[i].objectId == obj.objectId) {
          this.list[i].n--;
          localStorage.setItem('carlist', JSON.stringify(this.list))
          if (this.list[i].n == 0) {
            this.list.splice(i, 1)
            localStorage.setItem('carlist', JSON.stringify(this.list))
          }
        }
      }
    },
    nowNumberAdd(obj: any) {
      Object.values(this.all).forEach(item => {
        item.forEach(itm => {
          if (itm.objectId == obj.objectId) {
            itm.n++;
            console.log(this.all)
            // this.all = { ...this.all, ...obj }
            console.log(itm)
          }
        })
      });
    },
    nowNumberMunis(obj: any) {
      Object.values(this.all).forEach(item => {
        item.forEach(itm => {
          if (itm.objectId == obj.objectId) {
            itm.n--;
          }
        })
      });
    }
  }
})